// Copyright 2020 Poofee (https://github.com/Poofee)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------------
/*****************************************************************************
 *                                                                           *
 *  File:    valverelay.cpp                                                  *
 *                                                                           *
 *****************************************************************************
 *                                                                           *
 *  Authors: Poofee                                                          *
 *  Email:   poofee@qq.com                                                   *
 *  Address: https://github.com/Poofee                                       *
 *  Date:    2020年07月15日                                                   *
 *                                                                           *
 *****************************************************************************/
#include "valverelay.h"
#include "tetralheral.h"

ValveRelay::ValveRelay() {

}

ValveRelay::~ValveRelay() {

}


/*!
 * @brief      
 * @author     Poofee
 * @date       2020年9月18日
 * @param[out] 
 * @return     void  
**/
void ValveRelay::init() {
	/** 文件名，需不带扩展名 **/
	setFileName((char*)"../geo/valve/valve3dnewnoocc");	

	MAX_NONLINEARSTEPS = 100;
	/** 设置材料参数 **/
	CMaterial* air_material = new CMaterial;
	materialList.emplace_back(air_material);

	CMaterial* fesi_material = new CMaterial;
	materialList.emplace_back(fesi_material);
    fesi_material->sigma = 0;//1 / (2e-7);
	//fesi_material->miu *= 500;
    int num_bh = 45;
	fesi_material->BHpoints = num_bh;
	fesi_material->Hdata = (double*)malloc(num_bh * sizeof(double));
	fesi_material->Bdata = (double*)malloc(num_bh * sizeof(double));
	/** valve的BH曲线 **/
    double H[] = { 0,250,500,750,1000,1050,1266.66666666667,1483.33333333333,1700,1916.66666666667,2133.33333333333,2350,2566.66666666667,2783.33333333333,3000,3500,4604.16666666667,5708.33333333333,6812.50000000000,7916.66666666667,9020.83333333333,10125,11229.1666666667,12333.3333333333,13437.5000000000,14541.6666666667,15645.8333333333,16750,17854.1666666667,18958.3333333333,20062.5000000000,21166.6666666667,22270.8333333333,23375,24479.1666666667,25583.3333333333,26687.5000000000,27791.6666666667,28895.8333333333,30000,35000,101250,167500,233750,300000 };
    double B[] = { 0,0.753773251631365,1.39863688429926,1.82561188099240,2.03321790029929,2.05849607175444,2.13476159401776,2.17950111229574,2.20837477603254,2.22841287096933,2.24309907400825,2.25432202475208,2.26318417515254,2.27036873267275,2.27632023743496,2.28684061908444,2.30135204554018,2.31019558336258,2.31638771214449,2.32112830505603,2.32498850426272,2.32827467446237,2.33116585337267,2.33377365538223,2.33617127188482,2.33840869170545,2.34052121877186,2.34253449476720,2.34446759256544,2.34633499285869,2.34814788762635,2.34991506351470,2.35164351501571,2.35333887918225,2.35500574967005,2.35664790745680,2.35826849293680,2.35987013606292,2.36145505600124,2.36302513831697,2.36998816518522,2.45587006084705,2.53966395929684,2.62314997686049,2.70653251122877 };
	for (int ih = 0; ih < num_bh; ++ih) {
		fesi_material->Hdata[ih] = H[ih];
		fesi_material->Bdata[ih] = B[ih];
	}

	CMaterial* coil_material = new CMaterial;
	materialList.emplace_back(coil_material);
    coil_material->tau = 3250 / (6e-3 * 49.2e-3);
    Coil* coil = new Coil;
    coil->xyz       = 2;
    coil->direction = 1;
    coil->Nc        = 3250;
    coil->Rc        = 73;
    coil->Vc        = 24;
    coil->type      = RECTANGULARCOIL;
    coil->height    = 49.2e-3;
    coil->center[0] = 0;
    coil->center[1] = 0;
    coil->center[2] = 0;
    coil->r0        = 0;
    coil->width     = 6e-3;
    coil->xWidth    = 18e-3;
    coil->yWidth    = 22e-3;
    coil->Jor = coil->Vc /coil->Rc * coil->Nc / coil->width / coil->height;
    coil_material->coil = coil;

	/** 设置映射关系 **/
	setMaterial(1, fesi_material);/** 衔铁 **/
	setMaterial(2, coil_material);/** 线圈 **/
	setMaterial(3, fesi_material);/** 铁芯 **/
	setMaterial(4, air_material);/** 可压缩空气 **/
	/** 设置形变区域 **/
	setXiantieTag(1);
	setAirTag(4);
	setRemeshTag(4);

    motion.type = STRAIGHTMOTION;
    motion.v[0] = 0;
    motion.v[1] = 0;
    motion.v[2] = 1;

	Precision = 1e-6;

    ParallelThreads = 4;
}


/*!
 * @brief      valve静态初始化
 * @author     Poofee
 * @date       2020年10月24日
 * @param[out] 
 * @return     void  
**/
void ValveRelay::StaticInitial() {
	printf("Valve relay static initial\n");
	init();
    setPrefixName((char*)"static");
	/** 初始化时间步长 **/
	current_step = 0;
	min_time = 0;
	max_time = 0;
    timesteps.resize(14);

	/** 初始化变量 **/
	current_step = 0;
	initPhysicalVariables(timesteps.size());

	/** 设置位移增量列表 **/
    for (int i = 1; i < 14; ++i) {
		timesteps[i] = i;
        Ddisplacements[i] = -0.5e-3;/** 单位为m **/
	}
}

/*!
 * @brief      valve瞬态初始化
 * @author     Poofee
 * @date       2020年10月24日
 * @param[out] 
 * @return     void  
**/
void ValveRelay::TransientInitial() {
	printf("Valve relay transient initial\n");
	init();
    setPrefixName((char*)"eddy");

    /** 初始化时间步长 **/
    current_step = 0;
    min_time = 0;
    max_time = 105e-3;
    timesteps.emplace_back(0);
    for (double t = 0; t <= 105e-3; t += 1.5e-3) {
        timesteps.emplace_back(1.5e-3);
    }

    /** 初始化变量 **/
    current_step = 0;
    initPhysicalVariables(timesteps.size());
    /** 初始化位移，用来测试和验证 **/
    double dis[] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,-0.0072,-0.027,-0.06519,-0.12766,-0.21994,-0.34849,-0.51946,-0.73974,-1.01316,-1.34876,-1.7501,-2.22527,-2.78133,-3.38612,-4.05961,-4.79781,-5.51566,-6.24599,-6.3863,-6.3863,-6.3863,-6.3863,-6.3863,-6.3863,-6.3863,-6.3863,-6.3863,-6.3863,-6.3863,-6.3863,-6.3863,-6.3863,-6.3863,-6.3863,-6.3863,-6.3863,-6.3863,-6.3863,-6.3863,-6.3863,-6.3863,-6.3863,-6.3863,-6.3863,-6.3863,-6.3863,-6.3863,-6.3863,-6.3863,-6.3863,-6.3863,-6.3863,-6.3863,-6.3863,-6.3863,-6.3863,-6.3863 };
	for (int i = 1; i < timesteps.size(); ++i) {
        displacements[i] = dis[i]*unitRatio;/** 单位为m **/
        Ddisplacements[i] = displacements[i] - displacements[i - 1];
    }
}

